import { device_id, HUAWEI_IOT_URL } from './const';
import { project_id } from './const';
import http from '@ohos.net.http';

export interface PropertiesResp{
    device_id:string,
    shadow: Array<
    {
    service_id: string,
    desired: Object,
    reported: {
        properties: Object
        event_time: string
    },
    version: Number
}>
}
export const getProperties = (token:string)=>{
    const httpRequet = http.createHttp();
    return new Promise((resolve,reject)=>{
        httpRequet.request(`${HUAWEI_IOT_URL}/v5/iot/${project_id}/devices/${device_id}/shadow`,
            {
                header:{
                    "X-Auth-Token":token,
                    "Content-Type": "application/json",
                },
                method:http.RequestMethod.GET,
            }
        ).then(resp=>{
            if(resp.responseCode>=200 && resp.responseCode<300){
                const result = JSON.parse(resp.result as string) as PropertiesResp
                resolve(result)
            }else {
                reject(resp)
            }
        })
    })
}
